package com.ruoyi.service.impl;

import com.github.pagehelper.PageInfo;
import com.hundun.vision.api.common.PlainResult;
import com.hundun.vision.api.dto.FeatureDTO;
import com.hundun.vision.api.dto.FeatureTemplateDTO;
import com.hundun.vision.api.query.FeatureQuery;
import com.hundun.vision.api.request.FeatureRequest;
import com.hundun.vision.api.service.RPCFeatureService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ResultUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.service.FeatureService;
import com.ruoyi.service.FeatureTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ：RuoChen
 * @date ：16:28 2021/6/14
 * @description：特征服务实现类
 */
@Slf4j
@Component
public class FeatureServiceImpl implements FeatureService {

    @DubboReference(timeout = 60000, retries = 0)
    private RPCFeatureService featureService;

    @Autowired
    private FeatureTemplateService featureTemplateService;

    @Override
    public Boolean save(FeatureRequest request) {
        log.info("save feature,request:{}", request);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        request.setUserId(user.getUserId());
        PlainResult<Boolean> result = featureService.save(request);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return false;
    }

    @Override
    public Integer removeByIds(List<Long> featureIds) {
        log.info("remove feature by ids:{}", featureIds);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<Integer> result = featureService.removeByIds(user.getUserId(), featureIds);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return 0;
    }

    @Override
    public Boolean update(FeatureRequest request) {
        log.info("update feature,request:{}", request);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        request.setUserId(user.getUserId());
        PlainResult<Boolean> result = featureService.update(request);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return false;
    }

    @Override
    public List<FeatureTemplateDTO> getFeatureTemplates() {
        log.info("get feature templates");
        return featureTemplateService.list();
    }

    @Override
    public FeatureDTO getById(Long featureId) {
        log.info("get by featureId:{}", featureId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<FeatureDTO> result = featureService.getById(user.getUserId(), featureId);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public List<FeatureDTO> getByUserId(Long userId) {
        log.info("get feature by user id,userId:{}", userId);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        PlainResult<List<FeatureDTO>> result = featureService.getByUserId(user.getUserId());
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }

    @Override
    public PageInfo<FeatureDTO> search(FeatureQuery query) {
        log.info("search feature,query:{}", query);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        query.setUserId(user.getUserId());
        PlainResult<PageInfo<FeatureDTO>> result = featureService.search(query);
        if (result.isSuccess()) {
            return result.getData();
        } else {
            ResultUtils.error(result.getMessage());
        }
        return null;
    }
}
